<?php

namespace App\Jobs;

use App\Http\service\GzhService;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use App\Models\User as UserModel;
use App\Models\Gzh as GzhModel;
use Illuminate\Support\Facades\Log;
use App\Models\CouponMessage as CouponMessageModel;

class CouponMessage implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    private $data;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($uid)
    {
        $this->data=$uid;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $create=[
            "uid"=>$this->data["invite_uid"],
            "price"=>$this->data["price"],
            "state"=>0
        ];
        CouponMessageModel::create($create);
        Log::error($this->data);
        if (!$this->data["invite_uid"]){
            return;
        }
        $user=UserModel::where("id",$this->data["invite_uid"])->first();
        $openid=GzhModel::where("unionid",$user["unionid"])->first();
        $gzhService=new GzhService();
        if (!$user["nickname"]){
            $user["nickname"]="新用户";
        }


        if ($openid){
            $data=[
                "openid"=>$openid["openid"],
                "nickname"=>$user["nickname"],
                "price"=>$this->data["price"]
            ];
            $gzhService->invite_notice($data);
        }

    }
}
